import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { CustomerAccountStatementService } from './customer-account-statement.service';
import { ConfigDictListService } from '../../config/config-dict-list/config-dict-list.service'
import { DatePipe } from '@angular/common';
import { defalutImg } from 'src/app/comm';
import { NzModalService } from 'ng-zorro-antd/modal';
import { RevenueExpenditureArrs, profitExamineArrs, profitArrivalArrs } from '../status.pipe';
import { CommonService } from 'src/app/services-common/common.service';

@Component({
    selector: 'app-customer-account-statement',
    templateUrl: './customer-account-statement.component.html',
    styleUrls: ['./customer-account-statement.component.scss'],
    providers: [DatePipe]
})
export class CustomerAccountStatementComponent implements OnInit {
    routeParams: any = {};
    isCollapse: boolean = false;

    fallback: string = defalutImg;
    seachParams: any = {
        createTime: '', // 开始时间
        endTime: '', // 结束时间
        num: '', // 综合搜索
    };
    dateArrs: any = [];

    // 收支类型options
    incomeExpenditureOptions: any = [];
    incomeExpendSelected = [];
    // 收益审核状态options
    profitExamineOptions: any = [...profitExamineArrs];
    profitExamineSelected = [];
    // 收益到账状态options
    profitArrivalOptions: any = [...profitArrivalArrs];
    profitArrivalSeleected = [];

    // table
    tableParams: any = {
        tableLoading: false,
        page: 1,
        pageSize: 20,
        total: 0
    };
    tableData: any = [];

    constructor(
        private activatedRoute: ActivatedRoute,
        private message: NzMessageService,
        private services: CustomerAccountStatementService,
        private commonService: CommonService,
        private DictService: ConfigDictListService,
        private datePipe: DatePipe,
        private modal: NzModalService
    ) {
        this.activatedRoute.params.subscribe((res: any) => {
            this.routeParams = res || {};
        })
    }

    async ngOnInit() {
        await this.getIncomeExpenditure();
        this.loadData();
    }

    // 查询账户流水交易类型
    async getIncomeExpenditure() {
        const res: any = await this.commonService.dictListData('account_transaction_type');
        if (res.code == 0) {
            this.incomeExpenditureOptions = res.data.list || [];
        } else {
            this.incomeExpenditureOptions = [];
            this.message.error(res.message);
        }
    }

    async loadData() {
        try {
            let paramas = {
                userId: this.routeParams.id,
                ...this.seachParams,
                transactionType: this.incomeExpendSelected.join(','), // 收支类型
                auditStatus: this.profitExamineSelected.join(','), // 审核状态
                status: this.profitArrivalSeleected.join(','), // 到账状态
                isList: 0, // 是否是直接进入用户流水（1是0否）
                page: this.tableParams.page,
                pageSize: this.tableParams.pageSize
            }

            this.tableParams.tableLoading = true;
            const res: any = await this.services.queryWalletRecordTbale(paramas);
            this.tableParams.tableLoading = false;
            if (res.code == 0) {
                const { records, total }: any = res.data.list;
                this.tableData = records;
                this.tableParams.total = total;
            } else {
                this.message.error(res.message || '数据获取失败')
            }
        } catch (error: any) {
            this.tableParams.tableLoading = false;
            this.tableData = [];
            this.tableParams.total = 0;
            this.message.error(error)
        }
    }

    // 日期改变
    dateChange(result: any) {
        this.seachParams.createTime = result.length ? this.datePipe.transform(new Date(result[0]), 'yyyy-MM-dd') : '';
        this.seachParams.endTime = result.length ? this.datePipe.transform(new Date(result[1]), 'yyyy-MM-dd') : '';
    }

    // 查询
    queryData() {
        this.tableParams.page = 1;
        this.loadData();
    }
    // 重置
    resetData() {
        for (let key in this.seachParams) {
            this.seachParams[key] = '';
        }
        this.dateArrs = [];
        this.incomeExpendSelected = []; // 收支类型
        this.profitExamineSelected = []; // 审核状态
        this.profitArrivalSeleected = []; // 到账状态

        this.tableParams.page = 1;
        this.loadData()
    }

    // 当前页码改变时的回调函数
    onPageIndexChange(index: number) {
        this.tableParams.page = index;
        this.loadData()
    }
    // 页数改变时回调
    onPageSizeChange(sizeNum: number) {
        this.tableParams.page = 1;
        this.tableParams.pageSize = sizeNum;
        this.loadData()
    }

    // 收支类型文字filter
    typeFilter(type: any) {
        let tempObj: any = this.incomeExpenditureOptions.find((item: any) => {
            return item.content == type;
        })
        return tempObj?.name || '-';
    }
}
